package com.wisewin.circle.api.controller;

import com.wisewin.circle.api.controller.base.BaseController;
import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.entity.LoginUser;
import com.wisewin.circle.entity.bo.QueryParamBO;
import com.wisewin.circle.entity.db.CircleDO;
import com.wisewin.circle.entity.db.CircleFollowDO;
import com.wisewin.circle.entity.db.SysDictDO;
import com.wisewin.circle.service.ActivityService;
import com.wisewin.circle.service.CircleService;
import com.wisewin.circle.service.SysDictService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/circle")
public class CircleController extends BaseController {

    @Autowired
    private CircleService circleService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private SysDictService sysDictService;

    // 查询圈子权限设置
    @PostMapping("/queryCirclePower")
    public ResultDTO uploadCircleHeadUrl (HttpServletRequest request){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        List<SysDictDO> lstDict = sysDictService.querySysDictByDictType(SysDictDO.DICT_TYPE_QZQX);
        return ResultDTOBuilder.success(lstDict);
    }

    // 创建圈子上传图片
    @PostMapping("/uploadCircleHeadUrl")
    public ResultDTO uploadCircleHeadUrl (HttpServletRequest request, MultipartFile[] lstFile) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        return activityService.uploadFile(lstFile, loginUser.getId(), "circleHeadUrl");
    }

    // 创建圈子
    @PostMapping("/addCircle")
    public ResultDTO addCircle (HttpServletRequest request, @RequestBody CircleDO circleDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        circleDO.setUserId(loginUser.getId());
        return circleService.addCircle(circleDO);
    }

    // 关注圈子
    @PostMapping("/addCircleFollow")
    public ResultDTO addCircleFollow (HttpServletRequest request, @RequestBody CircleFollowDO circleFollowDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        circleFollowDO.setUserId(loginUser.getId());
        return circleService.addCircleFollow(circleFollowDO);
    }

    // 取消关注圈子
    @PostMapping("/cancleCircleFollow")
    public ResultDTO cancleCircleFollow (HttpServletRequest request, @RequestBody CircleFollowDO circleFollowDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        circleFollowDO.setUserId(loginUser.getId());
        return circleService.cancleCircleFollow(circleFollowDO);
    }

    // 查询圈子详细信息
    @PostMapping("/queryCircleDetail")
    public ResultDTO queryCircleDetail (HttpServletRequest request, @RequestBody CircleDO circleDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        circleDO.setUserId(loginUser.getId());
        return circleService.queryCircleDetail(circleDO);
    }

    // 查询圈子关注人列表
    @PostMapping("/queryCircleFollowUserList")
    public ResultDTO queryCircleFollowUserList(HttpServletRequest request, @RequestBody QueryParamBO queryParamBO){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setUserId(loginUser.getId());
        return circleService.queryCircleFollowUserList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }

    // 查询热门圈子
    @PostMapping("/queryCircleHotList")
    public ResultDTO queryCircleHotList (HttpServletRequest request) {
        String token = request.getHeader("token");
        if(!StringUtils.isEmpty(token)){
            // 获取登录用户
            LoginUser loginUser = super.getLoginUser(token);
            if (loginUser == null ) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
        }
        return circleService.queryCircleHotList();
    }

    // 查询圈子分类
    @PostMapping("/queryCircleTypeList")
    public ResultDTO queryCircleTypeList (HttpServletRequest request) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        return circleService.queryCircleTypeList();
    }

    // 查询圈子列表
    @PostMapping("/queryCircleList")
    public ResultDTO queryCircleList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(!StringUtils.isEmpty(token)){
            // 获取登录用户
            LoginUser loginUser = super.getLoginUser(token);
            if (loginUser == null ) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            queryParamBO.setUserId(loginUser.getId());
        }
        return circleService.queryCircleList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }

    // 查询圈子分类数据
    @PostMapping("/queryTypeCircleList")
    public ResultDTO queryTypeCircleList (HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer userId = null;
        if(!StringUtils.isEmpty(token)){
            // 获取登录用户
            LoginUser loginUser = super.getLoginUser(token);
            if (loginUser == null ) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            userId = loginUser.getId();
        }
        return circleService.queryTypeCircleList(userId);
    }

    // 查询我的圈子列表
    @PostMapping("/queryMyCircleList")
    public ResultDTO queryMyCircleList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setIfSelf(1);
        queryParamBO.setUserId(loginUser.getId());
        return circleService.queryCircleInfoList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }
}
